package io.renren.modules.business.service.impl;

import io.renren.common.utils.Constant;
import io.renren.modules.business.dao.BusInfoDao;
import io.renren.modules.business.entity.BusInfoEntity;
import io.renren.modules.business.service.BusInfoService;
import io.renren.modules.business.util.FilePathUtils;
import io.renren.modules.business.util.HtmlToPagesUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;


@Service("busInfoService")
public class BusInfoServiceImpl extends ServiceImpl<BusInfoDao, BusInfoEntity> implements BusInfoService {

    /**
     * 分页显示已发布信息记录
     * @param params
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<BusInfoEntity> page = this.page(
                new Query<BusInfoEntity>().getPage(params),
                new QueryWrapper<BusInfoEntity>()
        );
        return new PageUtils(page);
    }

    /**
     * 根据关键字查询发布信息
     * @param params
     * @return
     */
    @Override
    public PageUtils queryKey(Map<String, Object> params) {
        List<Map<String, Object>> list = new ArrayList<>();
        String key = (String) params.get("key");
        int check = checkType(key);
        if (check != -1) {
            //关键字是信息类型
            list = baseMapper.selectType(check);
        }else {
            //关键字不是信息类型
            list = baseMapper.selectKey(key);
        }
        int pageSize = 10;
        int currPage = Integer.valueOf((String) params.get("page"));
        int totalCount = list.size();
        int start = pageSize*(currPage-1);
        int end = (start + 10) > totalCount?totalCount:start + 10;
        List<Map<String, Object>> page = list.subList(start, end);
        return new PageUtils(page, totalCount, pageSize, currPage);
    }

    /**
     * 保存待发布信息
     * @param busInfoEntity
     */
    @Override
    public void saveInfo(BusInfoEntity busInfoEntity) {
        FilePathUtils util = new FilePathUtils();
        String caverUrl = busInfoEntity.getCover();
        //将内容渲染为html文件并保存路径
        String contentUrl = new HtmlToPagesUtils().toHtml(busInfoEntity.getContent());
        busInfoEntity.setCover(caverUrl);
        busInfoEntity.setUrl(contentUrl);
        busInfoEntity.setCreatTime(new Date());
        this.save(busInfoEntity);
    }

    /**
     * 删除已发布信息
     * @param infoIds
     */
    @Override
    public void deleteInfo(Long[] infoIds) {
        FilePathUtils filePathUtils = new FilePathUtils();
        String coverPath = filePathUtils.getCoverPath();
        String htmlPath = filePathUtils.getHtmlPath();
        for (Long id : infoIds) {
            //根据信息ID查询信息记录
            BusInfoEntity busInfoEntity = baseMapper.seleById(id);
            //删除信息封面
            String cover = coverPath + filePathUtils.parentFileName(busInfoEntity.getCover());
            deleFile(cover);
            //删除信息html文件
            String html = htmlPath + filePathUtils.parentFileName(busInfoEntity.getUrl());
            deleFile(html);
        }
        //删除信息记录
        baseMapper.deleteById(infoIds);
    }

    /**
     * 删除路径下的文件及文件夹
     * @param path
     */
    public void deleFile (String path){
        HtmlToPagesUtils htmlToPagesUtils = new HtmlToPagesUtils();
        if (htmlToPagesUtils.delAllFile(path)) {
            htmlToPagesUtils.delAllFile(path);
        }
    }

    /**
     * 检索信息类型
     * @param key
     * @return
     */
    public int checkType(String key) {
        for (Constant.MessageType messageType: Constant.MessageType.values()) {
            if (key.equals(messageType.getName())) {
                return messageType.getValue();
            }
        }
        return -1;
    }

    /**
     * 查询最新的四则热点文章，获取其封面与文章url
     * @return
     */
    public List<Map<String, Object>> indexSwiper() {
        List<Map<String, Object>> article = baseMapper.newInfo();
        return article;
    }
}